package com.ntschy.web.controller.bt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtDeclare;
import com.ntschy.bt.entity.dto.BtDeclareAddUpdateDTO;
import com.ntschy.bt.entity.dto.BtDeclarePageDTO;
import com.ntschy.bt.entity.enums.BtDeclareStatusEnum;
import com.ntschy.bt.entity.mapstruct.BtDeclareMapStruct;
import com.ntschy.bt.entity.valid.BtDeclareGroups;
import com.ntschy.bt.service.IBtDeclareService;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     专项申报 前端控制器
 * </p>
 *
 * @author ymj
 * @since 2021-01-05
 */

@RestController
@RequestMapping("/bt/declare")
@Api(tags = "专项申报接口")
@Validated
public class BtDeclareController extends BaseController<BtDeclare> {

    @Autowired
    private IBtDeclareService declareService;



    // * ================================================基础增删改查BEGIN============================================= *



    /**
     * 查询专项申报列表
     * @param pageDTO
     * @return
     */
    @ApiOperation(value = "查询专项申报列表",notes = "")
    @ApiImplicitParam(name = "queryDto",value = "专项申报列表查询实体类",required = true,dataType = "BtDeclarePageDTO")
    @PostMapping("/pageDeclare")
    public AjaxPageResult<BtDeclare> pageDeclare(@RequestBody BtDeclarePageDTO pageDTO){
        Page<BtDeclare> page = PageHelper.startPage(pageDTO.getCurrPage(),pageDTO.getPageSize());
        BtDeclare queryDeclare = BtDeclareMapStruct.BT_DECLARE_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<BtDeclare> list = declareService.pageDeclare(queryDeclare);
        return buildPageResult(list,page);
    }

    /**
     * 新增-保存专项申报
     *
     * @param addDto
     * @return
     */
    @ApiOperation(value ="新增保存专项申报",notes = "")
    @ApiImplicitParam(name = "addDto",value = "新增保存专项申报实体类",required = true,dataType = "BtDeclarePageDTO")
    @PostMapping("/addSaveDeclare")
    public AjaxResult<String> addSaveDeclare(@Validated(BtDeclareGroups.Add.class) @RequestBody BtDeclareAddUpdateDTO addDto){
        if(declareService.checkExistTitle(addDto.getTitle())){
            return AjaxResult.error(HttpStatus.ERROR,"申报标题已存在");
        }
        BtDeclare addDeclareInfo = BtDeclareMapStruct.BT_DECLARE_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(declareService.addDeclare(addDeclareInfo),"新增成功","新增失败");
    }

    /**
     * 新增发布专项申报
     * @param addDto
     * @return
     */
    @ApiOperation(value ="新增发布专项申报",notes = "")
    @ApiImplicitParam(name = "addSubmitDto",value = "新增发布专项申报实体类",required = true,dataType = "BtDeclarePageDTO")
    @PostMapping("/addSubmitDeclare")
    public AjaxResult<String> addSubmitDeclare(@Validated(BtDeclareGroups.Add.class) @RequestBody BtDeclareAddUpdateDTO addDto){
        if(declareService.checkExistTitle(addDto.getTitle())){
            return AjaxResult.error(HttpStatus.ERROR,"申报标题已存在");
        }
        BtDeclare addDeclareInfo = BtDeclareMapStruct.BT_DECLARE_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(declareService.addDeclare(addDeclareInfo),"发布成功","发布失败");
    }

    /**
     * 更新专项申报
     * @param updateDTO
     * @return
     */
    @ApiOperation(value = "更新专项申报",notes = "")
    @ApiImplicitParam(name = "updateDTO",value = "更新专项申报实体类",required = true,dataType = "BtDeclarePageDTO")
    @PostMapping("/updateDeclare")
    public AjaxResult<String> updateDeclare(@Validated(BtDeclareGroups.Update.class)@RequestBody BtDeclareAddUpdateDTO updateDTO){
        BtDeclare updateDeclare = BtDeclareMapStruct.BT_DECLARE_MAP_STRUCT.addUpdateDtoToDo(updateDTO);
        return toAjax(declareService.updateDeclare(updateDeclare),"更新成功","更新失败");
    }

    /**
     * 删除专项申报
     * @param id
     * @return
     */
    @ApiOperation(value = "删除专项申报",notes = "")
    @ApiImplicitParam(name = "Id",value = "专项申报ID",required = true,dataType = "String")
    @GetMapping("/deleteDeclare")
    public AjaxResult deleteDeclare( String id){
        return toAjax(declareService.deleteDeclare(id),"删除成功","删除失败");
    }
    // * =================================================基础增删改查END============================================== *

    /**
     * 获取下拉列表信息
     * @return
     */
    @PostMapping("/selectOptions")
    public AjaxResult selectOption(){
        List list = new ArrayList();
        list.add(BtDeclareStatusEnum.values());
        return AjaxResult.success("下拉信息查询成功",list);
    }

    /**
     * 申报详情
     * @param id
     * @return
     */
    @ApiOperation(value = "申报详情",notes = "")
    @ApiImplicitParam(name = "id",value = "申报详情",required = true,dataType = "String")
    @GetMapping("/fetchDeclare/{id}")
    public AjaxResult selectById(@PathVariable String id){
        return AjaxResult.success("查询详情成功",declareService.selectById(id));
    }

}
